package ee.logsimulator.ui;

import ee.logsimulator.Logsimulator;
import ee.logsimulator.Model;
import ee.logsimulator.data.GuildCalendar;
import ee.logsimulator.parser.HtmlParser;
import ee.logsimulator.parser.data.Raid;
import ee.logsimulator.ui.RaidToolTipUI.RaidToolTip;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToolTip;

import org.htmlparser.util.ParserException;

public class RaidPage extends MessageDialog {

  private JPanel panel;

  public RaidPage(final Logsimulator frame, final Model model, String url) throws ParserException, IOException {
    super(frame, "Select raid", true);

    drawPanel(frame, model, url);
  }

  protected void drawPanel(final Logsimulator frame, final Model model, String url) throws ParserException, IOException {
    if (panel != null) {
      remove(panel);
    }
    HtmlParser parser = new HtmlParser();
    final GuildCalendar guildPage = parser.parseGuildPage(url);

    panel = new JPanel();
    panel.setBackground(Color.BLACK);
    panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));

    JPanel p = new JPanel();
    p.setBackground(Color.BLACK);
    p.setLayout(new BoxLayout(p, BoxLayout.LINE_AXIS));

    if (guildPage.left != null) {
      JButton left = new JButton("<");
      left.setFocusPainted(false);
      left.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
          try {
            frame.setGuildPage(guildPage.left);
            drawPanel(frame, model, guildPage.left);
          } catch (Exception er) {
            er.printStackTrace();
          }
        }
      });
      p.add(left);
    }

    JLabel reportsIn = new JLabel("  " + guildPage.reportsIn + "  ");
    reportsIn.setForeground(Color.WHITE);
    reportsIn.setFont(reportsIn.getFont().deriveFont(30f));
    reportsIn.setAlignmentX(Component.CENTER_ALIGNMENT);
    p.add(reportsIn);

    if (guildPage.right != null) {
      JButton right = new JButton(">");
      right.setFocusPainted(false);
      right.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
          try {
            frame.setGuildPage(guildPage.right);
            drawPanel(frame, model, guildPage.right);
          } catch (Exception er) {
            er.printStackTrace();
          }
        }
      });
      p.add(right);
    }

    panel.add(p);

    for (final Raid raid : guildPage.raids) {
      JButton b = new JButton(raid.getName()) {
        private static final long serialVersionUID = 1L;

        @Override
        public JToolTip createToolTip() {
          RaidToolTip tip = new RaidToolTip(raid);
          tip.setComponent(this);
          tip.setOpaque(false);
          tip.setBorder(new javax.swing.border.EmptyBorder(0, 0, 0, 0));
          return tip;
        }

      };
      b.setToolTipText("bla bla");
      b.setFocusPainted(false);
      b.setAlignmentX(Component.CENTER_ALIGNMENT);
      b.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
          try {
            new BossPage(frame, model, "http://www.worldoflogs.com" + raid.link, raid);
            dispose();
          } catch (Exception e1) {
            frame.parseException(e1);
          }
        }
      });
      panel.add(b, BorderLayout.CENTER);
    }

    add(panel);
    pack();
    setResizable(false);
    setLocationRelativeTo(frame);
    setVisible(true);
  }

  private static final long serialVersionUID = 1L;

}
